/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grex.DT.tree;

import grex.Data.ArffTableModel;
import grex.DT.io.ItemSetReader;

import java.io.IOException;
import java.io.Serializable;
import java.util.Vector;

/**
 *
 * @author RIK
 */
public class Dataset implements Serializable{

    private ItemSet trainset = null;
    private ItemSet testset = null;
    private AttributeSet attributeSet = null;
    private int nrOfCols;
    private AttributeSet testAttributes =null;
    private SymbolicAttribute goalAttribute;
    private int targetIndex;
    ItemSet bootStrap, outofBag;
    public Dataset(ArffTableModel arff,boolean probColumns, String fold) {
        try {
            trainset = ItemSetReader.readTrainSet(arff.getTrainFoldAsString(fold));            
            attributeSet = trainset.attributeSet();
            testset = ItemSetReader.readTestSet(arff.getTestFoldAsString(fold),attributeSet);
            Vector testAttributesVector = new Vector();

            nrOfCols = arff.getNrOfCategorialColumns() + arff.getNrOfContinousColumns();
            targetIndex = nrOfCols;
            if (probColumns)
                nrOfCols -= arff.getNrOfTargetCategories();
            for (int c = 0; c < nrOfCols; c++) {
                testAttributesVector.add(attributeSet.attribute(c));
            }
            testAttributes = new AttributeSet(testAttributesVector);
            goalAttribute = (SymbolicAttribute) trainset.attributeSet().attribute(targetIndex);
            
            
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FileFormatException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * @return the trainset
     */
    public ItemSet getTrainset() {
        return trainset;
    }
    public ItemSet getBootstrap(){
        return bootStrap;
    }
    public ItemSet getOutofBag(){
        return outofBag;
    }
    public void createNewBootStrap(){
        bootStrap = new ItemSet(trainset.attributeSet());
        outofBag = new ItemSet(trainset.attributeSet());
        int index=0;
        boolean[] usedInBootStrap = new boolean[(int)trainset.size()];
        for(int i = 0; i< trainset.size();i++){
            index = (int) Math.floor(Math.random()*trainset.size());
            usedInBootStrap[index]=true;
            bootStrap.add(trainset.item(index));
        }
        for(int i = 0; i< trainset.size();i++){
            if(!usedInBootStrap[i])
                outofBag.add(trainset.item(i));
        }
//        System.out.println("outOfBag size" + outofBag.size());
//        System.out.println("bootStrap size" + bootStrap.size());
    }
    /**
     * @return the testset
     */
    public ItemSet getTestset() {
        return testset;
    }

    /**
     * @return the attributeSet
     */
    public AttributeSet getAttributeSet() {
        return attributeSet;
    }

    /**
     * @return the nrOfCols
     */
    public int getNrOfCols() {
        return nrOfCols;
    }

    /**
     * @return the targetCol
     */

    /**
     * @return the testAttributes
     */
    public AttributeSet getTestAttributes() {
        return testAttributes;
    }

    /**
     * @return the goalAttribute
     */
    public SymbolicAttribute getGoalAttribute() {
        return goalAttribute;
    }
}
